from flask import (
    Blueprint, flash, g, redirect, render_template, request, url_for,jsonify
)
from werkzeug.exceptions import abort

import concurrent.futures

import requests
from bs4 import BeautifulSoup
import json
import time
from datetime import datetime

bp = Blueprint('news', __name__)

@bp.route('/news',methods=('GET',))
def get_news():
    items = crawl_data()
    return items



headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}

def parse_html(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = []
    for sel in soup.select('div.category-wrap_iQLoo.horizontal_1eKyQ'):
        item = {}
        item['title'] = sel.select_one('div.c-single-text-ellipsis').text.strip()
        item['link'] = sel.select_one('div.hot-desc_1m_jR.large_nSuFU a')['href']
        item['briefs'] = sel.select_one('div.hot-desc_1m_jR.large_nSuFU').text.strip()
        item['hotCounts'] = sel.select_one('div.hot-index_1Bl1a').text.strip()
        item['imgUrl'] = sel.select_one('a img')['src']
        items.append(item)
    return items

def crawl_data():
    url = "https://top.baidu.com/board?tab=realtime"
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        items = parse_html(response.text)
        return items
    else:
        print("Failed to fetch data.")

